﻿
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Dto.QueryDto;
using Dora.Services.AdminUser.Dto.ViewDto;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using Surging.Core.Common;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Repository.Repositorys
{
    /// <summary>
    /// 机构文档表仓储层
    /// </summary>
    public class OrgDocRepository : SqlSugarRepository<OrgDocEntity, int>
    {
        private ISugarUnitOfWork _unitOfWork;
        public OrgDocRepository(ISugarUnitOfWork unitOfWork) : base(unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public async Task<int> CreateRange(List<OrgDocEntity> list)
        {
            var result = await _unitOfWork.ModifyClient.Insertable(list).ExecuteCommandAsync();
            return result;
        }

        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageDataList<OrgDocViewDto>> GetPageList(OrgDocQueryDto input)
        {
            var pagePredict = _unitOfWork.QueryClient.Queryable<OrgDocEntity,OrganizationEntity>((od, o) => new object[] { JoinType.Inner, od.OrgId == o.Id })
                .Where((od, o) => od.IsDeleted == 0)
                .WhereIF(input.OrgId.HasValue, (od, o) => od.OrgId == input.OrgId)
                .WhereIF(input.TopOrgId.HasValue, (od, o) => od.TopOrgId == input.TopOrgId)
                .WhereIF(input.Type.HasValue, (od, o) => od.Type == (int)input.Type.Value)
                .WhereIF(input.UploadStartTime.HasValue, (od, o) => od.CreatedTime >= input.UploadStartTime.Value)
                .WhereIF(input.UploadEndTime.HasValue, (od, o) => od.CreatedTime < input.UploadEndTime.Value.AddDays(1))
                .WhereIF(!string.IsNullOrWhiteSpace( input.Keywords), (od, o) => od.Name.Contains(input.Keywords.Trim()))
                ;
            var totalCount = await pagePredict.CountAsync();
            var pageList = await pagePredict
                .OrderBy((od, o) => od.IsTop, OrderByType.Desc)
                .OrderBy((od, o) => od.TopTime, OrderByType.Desc)
                .Select((od, o) => new OrgDocViewDto
                {
                    Id = od.Id,
                    TopOrgId=od.TopOrgId,
                    OrgId=od.OrgId,
                    Type = od.Type,
                    Name = od.Name,
                    DocExt = od.DocExt,
                    DocUrl = od.DocUrl,
                    PreviewUrl = od.PreviewUrl,
                    Sort = od.Sort,
                    Remark = od.Remark,
                    CreatedUserId = od.CreatedUserId,
                    CreatedUserName = od.CreatedUserName,
                    CreatedTime = od.CreatedTime,
                    OrgName = o.Name,
                    OrgShortName = o.ShortName
                })
                .ToPageListAsync(input.PageIndex, input.PageSize);
            var page = new PageDataList<OrgDocViewDto>(totalCount, pageList, input.PageIndex, input.PageSize);
            return page;
        }
    }
}
